package dS.ProjectGPS.LeicaOWI;

import javax.microedition.lcdui.*;
import java.util.Vector;

public class FormOwiDTP extends Form implements FormOwi {

  OWIAgent owiAgent = null;

  TextField ellipsoidName = new TextField("Ellipsoid Name", "", 30,
                                          TextField.ANY);
  TextField transformationName = new TextField("Transformation Name", "", 30,
                                               TextField.ANY);
  ChoiceGroup model = new ChoiceGroup("Model", ChoiceGroup.POPUP);
  TextField semiMajorAxis = new TextField("Semi-Major Axis", "", 50, TextField.ANY);
  TextField flatteningCoefficient = new TextField("Flattening Coefficient", "", 50, TextField.ANY);
  TextField scaleFactor = new TextField("Scale Factor", "", 9, TextField.DECIMAL);
  TextField deltaX = new TextField("Delta X", "", 9, TextField.DECIMAL);
  TextField deltaY = new TextField("Delta Y", "", 9, TextField.DECIMAL);
  TextField deltaZ = new TextField("Delta Z", "", 9, TextField.DECIMAL);
  TextField rotationX = new TextField("Rotation X", "", 9, TextField.DECIMAL);
  TextField rotationY = new TextField("Rotation Y", "", 9, TextField.DECIMAL);
  TextField rotationZ = new TextField("Rotation Z", "", 9, TextField.DECIMAL);
  StringItem note = new StringItem("", "If Molodensky-Badekas Model.", StringItem.PLAIN);
  TextField rotationOriginX = new TextField("Rotation Origin X", "", 9, TextField.DECIMAL);
  TextField rotationOriginY = new TextField("Rotation Origin Y", "", 9, TextField.DECIMAL);
  TextField rotationOriginZ = new TextField("Rotation Origin Z", "", 9, TextField.DECIMAL);

  public FormOwiDTP(OWIAgent o) {
    super("Datum Used");

    owiAgent = o;

    try {
      ndsInit();
    }
    catch (Exception ex) {
    }
  }

  public void ndsInit() throws Exception {
    append(ellipsoidName);
    append(transformationName);
    append(model);
    model.append("Bursa-Wolf", null);
    model.append("Molodensky-Badekas", null);
    append(semiMajorAxis);
    append(flatteningCoefficient);
    append(scaleFactor);
    append(deltaX);
    append(deltaY);
    append(deltaZ);
    append(rotationX);
    append(rotationY);
    append(rotationZ);
    append(note);
    append(rotationOriginX);
    append(rotationOriginY);
    append(rotationOriginZ);

  }

  public void update() {
    owiAgent.query(OWI.qsDTP());

    ellipsoidName.setString(OWI.dtpEllipsoidName);
    transformationName.setString(OWI.dtpDatumTransformtation);
    model.setSelectedIndex(OWI.dtpModel, true);
    semiMajorAxis.setString(OWI.dtpSemiMajorAxis);
    flatteningCoefficient.setString(OWI.dtpFlattening);
    scaleFactor.setString(OWI.dtpScaleFactor);
    deltaX.setString(OWI.dtpDeltaX);
    deltaY.setString(OWI.dtpDeltaY);
    deltaZ.setString(OWI.dtpDeltaZ);
    rotationX.setString(OWI.dtpRotX);
    rotationY.setString(OWI.dtpRotY);
    rotationZ.setString(OWI.dtpRotZ);
    rotationOriginX.setString(OWI.dtpMBRotOriX);
    rotationOriginY.setString(OWI.dtpMBRotOriY);
    rotationOriginZ.setString(OWI.dtpMBRotOriZ);
  }

  public void save() {

    String msgId = "DTP";
    Vector outputs = new Vector();

    // Setup SET parameters
    outputs.addElement(ellipsoidName.getString());
    outputs.addElement(transformationName.getString());
    outputs.addElement("" + model.getSelectedIndex());
    outputs.addElement(semiMajorAxis.getString());
    outputs.addElement(flatteningCoefficient.getString());
    outputs.addElement(scaleFactor.getString());
    outputs.addElement(deltaX.getString());
    outputs.addElement(deltaY.getString());
    outputs.addElement(deltaZ.getString());
    outputs.addElement(rotationX.getString());
    outputs.addElement(rotationY.getString());
    outputs.addElement(rotationZ.getString());

    if(model.getSelectedIndex() == 1){
      outputs.addElement(rotationOriginX.getString());
      outputs.addElement(rotationOriginY.getString());
      outputs.addElement(rotationOriginZ.getString());
    }

    String[] outputSA = new String[outputs.size()];
    for(int i=0; i<outputs.size(); i++){
      outputSA[i] = (String) outputs.elementAt(i);
    }

    //Send message and update form if successful
    int ackCode = (owiAgent.set(msgId, OWI.setMessage(outputSA)));
    Controller.showACKAlert(this, this, ackCode);
  }

}
